package com.tianji.learning.controller;

import cn.hutool.core.util.ObjectUtil;
import com.tianji.common.domain.dto.PageDTO;
import com.tianji.common.domain.query.PageQuery;
import com.tianji.common.utils.UserContext;
import com.tianji.learning.domain.dto.LearningPlanDTO;
import com.tianji.learning.domain.po.LearningLesson;
import com.tianji.learning.domain.vo.LearningLessonVO;
import com.tianji.learning.domain.vo.LearningPlanPageVO;
import com.tianji.learning.service.ILearningService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * TODO
 *
 * @Author whx
 * @Date 2024/11/22 18:17
 */
@RestController
@RequestMapping("/lessons")
@Slf4j
@Api(tags = "课表管理")
@RequiredArgsConstructor
public class LearningController {

    private final ILearningService learningService;

    /**
     * 查看我最近正在学习的课程
     * @return
     */
    @GetMapping("/now")
    @ApiOperation("查看我最近正在学习的课程")
    public LearningLessonVO getNow(){
        log.info("查看我最近正在学习的课程");
        return learningService.now();
    }

    /**
     * 分页查询我的课表
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询我的课表")
    public PageDTO<LearningLessonVO> pageDTO(PageQuery pageQuery){
        log.info("分页查询我的课表");
        return learningService.page(pageQuery);
    }

    /**
     * 根据id查询某课程学习状态
     * @param courseId
     * @return
     */
    @GetMapping("/{courseId}")
    @ApiOperation("根据id查询某课程学习状态")
    public LearningLessonVO getCourseId4LearningLessonVo(@PathVariable Long courseId){
        log.info("根据id查询某课程学习状态，课程id：{}", courseId);
        return learningService.getCourseId4LearningLessonVo(courseId);
    }

    /**
     * 删除课表中的指定课程
     */
    @DeleteMapping("{courseId}")
    @ApiOperation("删除课表中的指定课程")
    public void deleteById(@PathVariable Long courseId){
        log.info("删除课表中的指定课程，课程id：{}", courseId);
        learningService.removeById(courseId);
    }

    /**
     * 统计课程学习人数
     * @param courseId
     * @return
     */
    @GetMapping("/{courseId}/count")
    @ApiOperation("统计课程学习人数")
    public Integer countLearningLessonByCourse(@PathVariable("courseId") Long courseId){
        log.info("统计课程学习人数，课程id：{}", courseId);

        return learningService.lambdaQuery()
                .eq(LearningLesson::getCourseId, courseId)
                .count();
    }

    /**
     * 校验当前用户是否可以学习当前课程
     * @param courseId 课程id
     * @return lessonId，如果是报名了则返回lessonId，否则返回空
     */
    @GetMapping("/{courseId}/valid")
    public Long isLessonValid(@PathVariable("courseId") Long courseId){
        log.info("校验当前用户是否可以学习当前课程，课程id：{}", courseId);
        Long userId = UserContext.getUser();
        // 查询用户课表，做一个非空和状态的判断
        LearningLesson learningLesson = learningService.lambdaQuery()
                .eq(LearningLesson::getUserId, userId)
                .eq(LearningLesson::getCourseId, courseId)
                .list().get(0);

        if(!ObjectUtil.isNull(learningLesson) && learningLesson.getStatus() != 3){
            return courseId;
        }

        return null;
    }


    /**
     * 创建学习计划
     * @param learningPlanDTO
     */
    @PostMapping("/plans")
    @ApiOperation("创建学习计划")
    public void createPlans(@RequestBody LearningPlanDTO learningPlanDTO){
        log.info("创建学习计划，课程id：{}，学习频率：{}", learningPlanDTO.getCourseId(), learningPlanDTO.getFreq());
        learningService.createPlans(learningPlanDTO);
    }

    /**
     * 查询我的学习记录
     * @param pageQuery
     * @return
     */
    @GetMapping("/plans")
    @ApiOperation("查询我的学习记录")
    public LearningPlanPageVO pageMyStudyPlans(PageQuery pageQuery){
        log.info("查询我的学习记录：{}", pageQuery);
        return learningService.pageMyStudyPlans(pageQuery);
    }



}
